﻿using System;

namespace Bricks
{
    
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKernel"></typeparam>
    public abstract class FacadeBase<TKernel> : IFacade<TKernel>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="kernelAccessor"></param>
        protected FacadeBase(Func<TKernel> kernelAccessor)
        {
            if (kernelAccessor == null)
            {
                throw new ArgumentNullException("kernelAccessor");
            }
            _kernelAccessor = kernelAccessor;
        }

        /// <summary>
        /// 
        /// </summary>
        private readonly Func<TKernel> _kernelAccessor;

        /// <summary>
        /// 
        /// </summary>
        protected virtual TKernel Kernel
        {
            get { return _kernelAccessor.Invoke(); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        public void Execute(Action<TKernel> action)
        {
            Execute<bool>(
                kernel =>
                {
                    action.Invoke(kernel);

                    return true;
                });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public virtual TResult Execute<TResult>(Func<TKernel, TResult> action)
        {
            return action.Invoke(Kernel);
        }
    }
}
